#include "GAF_Face.h"

namespace GAF
{
	Face::Face()
	{
		m_nGType	= GRAPHICS_FACE;
		m_pimgFace	= NULL;
	}

	Face::Face(unsigned int nWidth, unsigned int nHeight)
	{
		GetSize_a().nWidth = nWidth;
		GetSize_a().nHeight = nHeight;
		m_pimgFace = Image::Create(nWidth, nHeight);
	}

	Face::~Face()
	{
		if(m_pimgFace != NULL)
			Image::Destroy(m_pimgFace);

		m_lstGraphicses.clear();
	}

	bool Face::Init(Bundle* pbdlParam)
	{
		return OnInit(pbdlParam);
	}

        Pointer Face::Foreach(bool isFirst)
        {
                if(isFirst)
                        ms_citCur = m_lstGraphicses.begin();
                else
                        ms_citCur++;

                if(ms_citCur == m_lstGraphicses.end())
                        return NULL;

                return (Pointer)*ms_citCur;
        }

        UINT Face::GetCount()
        {
                return m_lstGraphicses.size();
        }

        Graphics* Face::GetChildAt(UINT nID)
        {
                if(nID >= m_lstGraphicses.size())
                        return NULL;

                GraphicsList::iterator _it = m_lstGraphicses.begin();
                for(UINT _n = 0; _n < nID; _n++)
                {
                        _it++;
                }

                return *_it;
        }

	void Face::AddGraphics(Graphics* pGraphics, bool isBack)
	{
		if(pGraphics == NULL)
			int ddd = 0;

		GAF_CHECK(pGraphics != NULL);

		if(pGraphics->GetDockGroup() == this)
			RemoveGraphics(pGraphics);

		if(isBack)
			m_lstGraphicses.push_front(pGraphics);
		else
			m_lstGraphicses.push_back(pGraphics);

		pGraphics->DockGroup(this);

		RefreshSize(pGraphics);
	}

 	void Face::AddGraphicses(Graphics** pGraphics, COUNT nCount, bool isBack)
 	{
 		for(UINT _i = 0; _i < nCount; _i++)
 			AddGraphics(pGraphics[_i], isBack);
 	}

	void Face::RemoveGraphics(Graphics* pGraphics)
	{
		GAF_CHECK(pGraphics != NULL);

		m_lstGraphicses.remove(pGraphics);
		pGraphics->DockGroup(NULL);

                RefreshSize();
	}

	void Face::ClearGraphics()
	{
		if(m_lstGraphicses.size() > 0)
		{
// 			for(GraphicsList::const_iterator _cit = m_lstGraphicses.begin();
// 				_cit != m_lstGraphicses.end(); _cit++)
// 			{
// 				(*_cit)->DockGroup(NULL);
// 			}

                        GraphicsList::const_iterator _cit;
                        while(m_lstGraphicses.size() != 0)
                        {
                                _cit = m_lstGraphicses.begin();
                                (*_cit)->DockGroup(NULL);
                        }

			m_lstGraphicses.clear();
		}
	}

	void Face::RefreshSize(Graphics* pAddChild)
	{
		if(IsIgnoreSize())
			return;

		Rect _rAreaThis = {0, 0, 0, 0};

                if(pAddChild == NULL)
                {
                        Graphics* _pChild = NULL;
                        for(GraphicsList::const_iterator _cit = m_lstGraphicses.begin();
                                _cit != m_lstGraphicses.end(); _cit++)
                        {
                                _pChild = *_cit;

                                if(_pChild->IsIgnoreSize() == false)
                                {
                                        RefreshByChild(_pChild, _rAreaThis);
                                }
                        }
                }
                else
                {
                        _rAreaThis.Set(0, 0, GAF_ARG_SIZE(GetSize_a()));
                        RefreshByChild(pAddChild, _rAreaThis);
                }

		SetSize(_rAreaThis.nWidth, _rAreaThis.nHeight);
	}

        void Face::RefreshByChild(Graphics* pChild, Rect& rAreaThis)
        {
                Rect _rAreaChild = {0, 0, 0, 0};

                if(pChild->GetGType() == GRAPHICS_FACE)
                        ((Face*)pChild)->RefreshSize();

                int _nX = GetPosXByAnchor(pChild->GetLocation_a().nX, pChild->GetSize_a().nWidth, pChild->GetAnchor_a());
                int _nY = GetPosXByAnchor(pChild->GetLocation_a().nY, pChild->GetSize_a().nHeight, pChild->GetAnchor_a());

                _rAreaChild.Set(_nX, _nY, GAF_ARG_SIZE(pChild->GetSize_a()));
                rAreaThis = rAreaThis.Compose(_rAreaChild);
        }
}
